package br.com.hermes.bt;

import java.util.HashSet;
import java.util.Set;
import java.util.Vector;

/**
 * HermesBlue
 */
public abstract class HermesBlue implements HmRemoteDeviceListener{

  private HmBluetoothComm comunication;
  private Vector<HmBluetoothDevice> devicesDiscovered;
 	private Set<HmListener> listeners;
 	private HmStream hmStream;
   
   
   public HermesBlue() {
  	 devicesDiscovered = new Vector<HmBluetoothDevice>();
  	 listeners         = new HashSet<HmListener>();
   }
   
	 /**
	 * @return the devicesDiscovered
	 */
	public Vector<HmBluetoothDevice> getDevicesDiscovered() {
		return devicesDiscovered;
	}

	/**
	 * @param devicesDiscovered the devicesDiscovered to set
	 */
	public void setDevicesDiscovered(Vector<HmBluetoothDevice> devicesDiscovered) {
		this.devicesDiscovered = devicesDiscovered;
	}


	/**
	 * @return the comunication
	 */
	public HmBluetoothComm getComunication() {
		return comunication;
	}

	/**
	 * @param comunication the comunication to set
	 */
	public void setComunication(HmBluetoothComm comunication) {
		this.comunication = comunication;
	}

	/**
	 * @return the hmStream
	 */
	public HmStream getHmStream() {
		return hmStream;
	}

	/**
	 * @param hmStream the hmStream to set
	 */
	public void setHmStream(HmStream hmStream) {
		this.hmStream = hmStream;
	}

	/**
	 * @param buffer to write
	 */
	public void write(byte buffer[]) throws HmException{
		 if(comunication!=null){
			 comunication.write(buffer);
		 }
	}
	
	
	public void notifyDiscoveryFinished(Vector<HmBluetoothDevice> devicesDiscovered){
		this.devicesDiscovered = devicesDiscovered;
		notifyAllDevicesDiscovered();
	}
	
	
	private void notifyAllDevicesDiscovered() {
		for (HmListener hermesListener : listeners) {
			hermesListener.notifyDevicesDiscovered(this.devicesDiscovered);
		}
	}
	
	public void addHermesListener(HmListener listener){
		listeners.add(listener);
	}
	
	public void notifyAllMessage(E_MessageType type, String message){
		for (HmListener hermesListener : listeners) {
			hermesListener.notifyMessage(type, message);
		}
	}

	public void notifyAllData(byte data[]){
		for (HmListener hermesListener : listeners) {
			hermesListener.notifyData(data);
		}
	}

	public void notifyAllIsRead(){
		for (HmListener hermesListener : listeners) {
			hermesListener.notifyIsRead();
		}
	}
	
  public abstract void startDiscovery();
  public abstract void connect(HmBluetoothDevice device) throws HmException;
  public abstract void initService();
  public abstract void startService();
  
}
